package com.tim.framework.util;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;

import com.tim.framework.common.PageProperty;

public class CommonTools {
	//处理 where和order查询语句 使传入sql兼容带where 和不带where
	public static String makeSqlWhereOrder(String _where,String _order){
		String sql="";
		if(!StringUtils.isEmpty(_where)){
			sql+=" ";
			if(_where.trim().indexOf("where")!=0){
				sql+="where ";
			}
			sql+= _where.trim();
		}
		if(!StringUtils.isEmpty(_order)){
			sql+=" ";
			if(_order.trim().indexOf("order by")!=0){
				sql+="order by ";
			}
			sql+=_order.trim();
		}
		return sql;
	}
	//设置hql参数列表
	public  static void setParameterList(Query query, PageProperty pp) {
		if(pp.getParameterList() != null) {
			for(int i = 0; i < pp.getParameterList().size(); i++) {
				query.setParameter(i, pp.getParameterList().get(i));
			}
		}
	}
	public static String URLEncode(String str){
		String reStr=str;
		try {
			reStr=URLEncoder.encode(reStr,"UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return reStr;
	}
	public static String URLDecode(String str){
		String reStr=str;
		try {
			reStr=URLDecoder.decode(reStr,"UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return reStr;
	}
}
